//
// Created by 11510 on 2024/7/4.
//

#ifndef DAY71_THREAD_H
#define DAY71_THREAD_H

#include <pthread.h>

class Thread {
private:
    pthread_t _thid;
    bool _isRunning;
public:
    Thread() : _isRunning(false) {
    }

    void start() {
        if(!_isRunning){
            _isRunning = true;
            pthread_create(&_thid, NULL, threadFunc, this);
        }
    }

    void stop() {
        if (_isRunning) {
            pthread_join(_thid, NULL);
            _isRunning = false;
        }
    }

    virtual void run() = 0;

    ~Thread() {
        stop();
    }

private:
    static void *threadFunc(void *arg) {
        Thread *thread = (Thread *) arg;
        thread->run();
        return NULL;
    }
};


#endif //DAY71_THREAD_H
